﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;

namespace Portal.CMS
{
    public class DocumentItems : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "DocumentItems";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("Title", "Danh sách tài liệu");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(DocumentItems));
            string DocumentId = System.Web.HttpContext.Current.Request["DocumentId"];

            PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "Document");
            DocumentCls DocumentCls = new DocumentBLL().CreateModel(Environments.ActiveLanguage, DocumentId);
            Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowDocumentItems", DocumentId, DocumentCls.Owner, true);


            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "Document.js") +
                 "<div class=cssFormContent id=divFormContent>" +
                    GetHtml(Environments.ActiveLanguage, DocumentId, "", 0) +
                 "</div>";


            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string ActiveLanguage, string DocumentId, string Keyword, int CurrentPageIndex)
        {
            string RetHtml = "";
            DataTable dtDocumentItems = new DocumentBLL().ReadingDocumentItems(DocumentId, Keyword);
            int TotalRow = dtDocumentItems.Rows.Count;

            DocumentCls DocumentCls = new DocumentBLL().CreateModel(Environments.ActiveLanguage, DocumentId);
            if (DocumentCls == null)
            {
                return Environment.Environments.GetRecordNotFound();
            }

            string SelectLanguageText =
             "<select id=divSelectLanguage onchange=javascript:DetectDocumentDescription('" + DocumentId + "');>";
            string[] Languages = Environment.Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string TabModuleId = "D4711A72";
            int StartIndex = 0;
            int EndIndex = 0;
            string PagingText = new PagingCls().GetPaging(TabModuleId, dtDocumentItems.Rows.Count, 5, 5, CurrentPageIndex, "ClientJumpDocumentItem", ref StartIndex, ref EndIndex);

            RetHtml += "     <div id=divProcessing></div>" +
                             "<div class=cssDocumentItems>" +
                             "<div>" + DocumentCls.Subject + "&nbsp;" + SelectLanguageText + "&nbsp;" + GetText("Keyword", "Từ khóa") + ":<input id=txtKeyword value=\"" + Keyword + "\"> <input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Refresh", "Đọc lại") + "\" onclick=javascript:ClientReading('" + DocumentId + "');>&nbsp;<input type=button onclick=javascript:UploadNewImageFromListItems('" + DocumentId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UploadNew", "Upload mới") + "\"><input type=button onclick=javascript:DoSyncAll('" + DocumentId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "SycXmlData", "Cập nhật máy chủ") + "\"></div>" +
                             "<input type=hidden id=txtDocumentId value=\"" + DocumentId + "\">" +
                             "<input type=hidden id=txtLanguage value=\"" + ActiveLanguage + "\">";

            if (dtDocumentItems.Rows.Count == 0)
            {
                RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
            }
            else
            {
                RetHtml +=
                             PagingText +
                             "<div id=divProcessingDocumentItem></div>" +
                             "<table>";

                for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                {
                    string belongSite = (string)dtDocumentItems.Rows[iIndex]["belongSite"];
                    DocumentItemCls DocumentItemCls = new DocumentItemCls();
                    DocumentItemCls.DocumentItemId = (string)dtDocumentItems.Rows[iIndex]["DocumentItemId"];
                    DocumentItemCls.DocumentItemName = (string)dtDocumentItems.Rows[iIndex]["DocumentItemName"];
                    DocumentItemCls.Url = (string)dtDocumentItems.Rows[iIndex]["Url"];
                    try
                    {
                        DocumentItemCls.FileUpload = (string)dtDocumentItems.Rows[iIndex]["FileUpload"];
                    }
                    catch { }
                    try
                    {
                        DocumentItemCls.Description = (string)dtDocumentItems.Rows[iIndex]["Description"];
                    }
                    catch { }
                    DocumentItemCls.Target = (string)dtDocumentItems.Rows[iIndex]["Target"];
                    DocumentItemCls.IndexOrder = (int)dtDocumentItems.Rows[iIndex]["IndexOrder"];
                    try
                    {
                        DocumentItemCls.IsPublishing = (int)dtDocumentItems.Rows[iIndex]["IsPublishing"];
                    }
                    catch { }

                    RetHtml += ServerSideNewLinkItem(belongSite, iIndex, DocumentId, DocumentItemCls);
                }
                RetHtml += "</table>";
            }
            RetHtml += "</div>";
            
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideNewLinkItem(string belongSite, int Index, string DocumentId, DocumentItemCls DocumentItemCls)
        {
            //string ImageUrl = Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/Document/Images/logo.jpg";
            string SrcDoc = "javascript:ClientViewAttachFile('" + DocumentItemCls.DocumentItemId + "');";// Environments.HttpMediaRoot + "/media/" + Environments.PortalAlias + "/Document/" + belongSite + "/Archives/" + DocumentItemCls.FileUpload;

            string PublishingText = "<input id=txtPublishing_" + DocumentItemCls.DocumentItemId + " type=button onclick=javascript:ClientPublishing('" + DocumentItemCls.DocumentItemId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "Publishing", "Xuất bản") + "("+(Index+1)+ ")\">";
            if (DocumentItemCls.IsPublishing == 1)
            {
                PublishingText = "<input id=txtPublishing_" + DocumentItemCls.DocumentItemId + " type=button onclick=javascript:ClientUnPublishing('" + DocumentItemCls.DocumentItemId + "'); value=\"" + ResourceBLL.GetText(WebPartId, "UnPublishing", "Ngừng xuất bản")+" ("+(Index+1)+ ")\">";
            }

            string
            Html = "<tr>" +
                //"<td valign=top><a href=\"javascript:ClientViewAttachFile('" + DocumentItemCls.DocumentItemId + "');\">"+ResourceBLL.GetText(WebPartId,"Preview","Xem file")+"</td>" +
                   "        <td valign=top>" +
                   "        <table>" +
                   "        <tr>" +
                   "            <td>#</td>" +
                   "            <td class=cssDocumentItemsCol1><input type=hidden value=\"" + DocumentItemCls.DocumentItemId + "\" id=\"txtItemId_" + Index + "\">(" + (Index + 1) + ")</td>\r\n" +
                   "        </tr>" +
                   "        <tr>" +
                   "            <td>Văn bản:</td>" +
                   "            <td class=cssDocumentItemsCol1><input class=cssDocumentItemTextBox value=\"" + DocumentItemCls.DocumentItemName + "\" id=\"txtItemName_" + Index + "\"></td>\r\n" +
                   "        </tr>" +
                   "        <tr>" +
                   "            <td>Url</td>" +
                   "            <td class=cssDocumentItemsCol2><input class=cssDocumentItemTextBox value=\"" + DocumentItemCls.Url + "\" id=\"txtItemUrl_" + Index + "\"></td>\r\n" +
                   "        </tr>" +
                   "        <tr>" +
                   "            <td>Target:</td>" +
                   "            <td class=cssDocumentItemsCol3><input value=\"" + DocumentItemCls.Target + "\" class=cssDocumentItemTextBox id=\"txtItemTarget_" + Index + "\"></td>\r\n" +
                   "        </tr>" +
                   "        <tr>" +
                   "            <td>Mô tả:</td>" +
                   "            <td class=cssDocumentItemsCol4><textarea id=\"txtItemDescription_" + Index + "\" class=cssDocumentItemTextBox>" + DocumentItemCls.Description + "</textarea></td>\r\n" +
                   "        </tr>" +
                   "        <tr>" +
                   "            <td>Thứ tự</td>" +
                   "            <td class=cssDocumentItemsCol5><input value=\"" + DocumentItemCls.IndexOrder.ToString() + "\" class=cssDocumentItemTextBox value=\"" + (Index + 1) + "\" id=\"txtItemIndexOrder_" + Index + "\"></td>\r\n" +
                   "        </tr>" +
                   "        <tr>" +
                   "            <td></td>" +
                   "            <td class=cssDocumentItemsCol6>" +
                   "                    <input onclick=javascript:PermissionDocumentItem('" + DocumentItemCls.DocumentItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "PermissionButton", "Phân quyền") + " (" + (Index + 1) + ")\">" +
                   PublishingText +
                   "                    <input onclick=javascript:DeleteDocumentItem('" + DocumentId + "','" + DocumentItemCls.DocumentItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "DeleteButton", "Xoá") + " (" + (Index + 1) + ")\">" +
                   "                    <input onclick=javascript:SaveDocumentItem(" + Index + ",'" + DocumentId + "','" + DocumentItemCls.DocumentItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "SaveButton", "Ghi lại") + " (" + (Index + 1) + ")\">" +
                   "                    <input onclick=javascript:UploadNewDocumentItemForUpdate('" + DocumentId + "','" + DocumentItemCls.DocumentItemId + "'); type=button value=\"" + ResourceBLL.GetText(WebPartId, "UpdateButton", "Upload lại tài liệu") + " ("+(Index+1)+")\">" +
                   "                    <input onclick=javascript:ClientViewAttachFile('" + DocumentItemCls.DocumentItemId + "'); type=button value=\""+GetText("Download","Xem tài liệu")+ " ("+(Index+1)+")\">"+
                   "                    <input onclick=javascript:ClientSyncDocumentItem('" + DocumentItemCls.DocumentItemId + "'); type=button value=\"" + GetText("Sync", "Sync") + " (" + (Index + 1) + ")\">" +
                   "            </td>\r\n" +
                   "        </tr>" +
                   "    </table>" +
                   "    </td>" +
                   "</tr>";
            return Html;
        }


        [AjaxPro.AjaxMethod]
        public string GetShowAppendForm(string DocumentId)
        {
            string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddDocument().WebPartId),
                        new ParamCls("DocumentId",DocumentId)
                    });
            return AppendUrl;
        }



        [AjaxPro.AjaxMethod]
        public string ServerGetDescription(string ActiveLanguage, string DocumentId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                DocumentCls DocumentCls = new DocumentBLL().CreateModel(ActiveLanguage, DocumentId);
                return DocumentCls.Description;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideCheckDeleteDocumentItem(string DocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteDocumentItem", DocumentItemId, owner, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteDocumentItem(string DocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteDocumentItem", DocumentItemId, owner, true);


                new DocumentBLL().DeleteDocumentItem(null, DocumentItemId);
                return ResourceBLL.GetText(WebPartId, "DeleteOk", "Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetAddDocumentItemUrl(string DocumentId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "Document");
                DocumentCls DocumentCls = new DocumentBLL().CreateModel(Environments.ActiveLanguage, DocumentId);
                string owner = DocumentCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddDocumentItem", DocumentCls.DocumentId, owner, true);


                string AppendUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UploadDocument().WebPartId),
                        new ParamCls("DocumentId",DocumentId)
                    });
                return AppendUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveDocumentItem(
            string ActiveLanguage,
            string DocumentId,
            string DocumentItemId,
            string DocumentItemName,
            string Url,
            string Target,
            string Description,
            string IndexOrder)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "SaveDocumentItem", DocumentItemId, owner, true);

                DocumentItemCls.DocumentItemName = DocumentItemName;
                DocumentItemCls.Url = Url;
                DocumentItemCls.Target = Target;
                DocumentItemCls.Description = Description;
                DocumentItemCls.IndexOrder = int.Parse(IndexOrder);

                new DocumentBLL().SaveDocumentItem(null, ActiveLanguage, DocumentId, DocumentItemCls);
                return ResourceBLL.GetText(WebPartId, "SaveDocumentItemsOk", "Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckSaveDocumentItem(
            string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "SaveDocumentItem", DocumentItemId, owner, true);
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideCheckPublishing(string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "Publishing", DocumentItemId, owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSidePublishing(string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "Publishing", DocumentItemId, owner, true);

                new DocumentBLL().Publishing(null, DocumentItemId);
                return ResourceBLL.GetText(WebPartId, "PublishingOk", "Phát hành thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckUnPublishing(string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UnPublishing", DocumentItemId, owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideUnPublishing(string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UnPublishing", DocumentItemId, owner, true);

                new DocumentBLL().UnPublishing(null, DocumentItemId);
                return ResourceBLL.GetText(WebPartId, "UnPublishingOk", "Ngừng phát hành thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetPublishingCaption()
        {
            try
            {
                return ResourceBLL.GetText(WebPartId, "Publishing", "Phát hành");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetUnPublishingCaption()
        {
            try
            {
                return ResourceBLL.GetText(WebPartId, "UnPublishing", "Ngừng phát hành");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmPublishingMsg", "Bạn đã chắc chắn phát hành chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmUnPublishingMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmUnPublishingMsg", "Bạn đã chắc chắn ngừng phát hành chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetDocumentItemUrl(string DocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionDocumentItem", DocumentItemId, owner, true);

                return Environment.Permission.GetPermissionUrl(new Document().WebPartId, DocumentItemId, "DocumentItems");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideSyncDocumentItem(string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new DocumentBLL().SyncDocumentItem(DocumentItemId);
                return ResourceBLL.GetText(WebPartId, "SyncOk", "Đồng bộ với máy chủ Web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string GetSyncProcessing()
        {
            return ResourceBLL.GetText(WebPartId, "SyncProcessing", "Đang cập dữ liệu cho máy chủ public...");
        }

        [AjaxPro.AjaxMethod]
        public string GetUploadUpdateImage(string DocumentId,string DocumentItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new Document().PermissionTypes, "DocumentItems");
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                string owner = DocumentItemCls.Owner;
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ReUploadDocumentItem", DocumentItemId, owner, true);

                return Screen.BuildDialogUrl(new ReDocumentUpload().WebPartId, new ParamCls[]{
                new ParamCls("DocumentItemId",DocumentItemId)
            });

            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideDownloadItem(string DocumentItemId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                DocumentItemCls DocumentItemCls = new DocumentBLL().CreateModelItem(Environments.ActiveLanguage, DocumentItemId);
                //PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new LawDocument().PermissionTypes, "LawDocumentItems");
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DownloadItem", LawDocumentItemId, lawDocumentItemCls.Owner, true);

                byte[] b = Environment.BobController.ReadBlobField(Environments.ConnectionString, "TableDocumentItem", "DocumentContent", "DocumentItemId", DocumentItemId);
                if (b == null) throw new Exception(ResourceBLL.GetText(WebPartId, "hasNotAttachedFile", "Không có tài liệu gắn kèm!"));
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias);
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias + "\\Document");
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias + "\\Document\\" + DocumentItemCls.belongSite);
                System.IO.Directory.CreateDirectory(Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias + "\\Document\\" + DocumentItemCls.belongSite + "\\Archives");

                string PathSave = Environments.WorkingPath + "\\temp\\" + Environments.PortalAlias + "\\Document\\" + DocumentItemCls.belongSite + "\\Archives\\" + DocumentItemCls.FileUpload;

                try
                {
                    System.IO.File.Delete(PathSave);
                }
                catch { }
                Environment.BobController.WriteBytesToFile(b, PathSave);
                string FileUrl = Environments.HttpMediaRoot + "/temp/" + Environments.PortalAlias + "/Document/" + DocumentItemCls.FileUpload;
                return FileUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}